﻿namespace YnabCsvConverter.Core.Tests
{
   using Converters;
   using Converters.Betterment;
   using NUnit.Framework;

   /// <summary>
   /// Unit Test for the converter scanner
   /// </summary>
   [TestFixture]
   public class ConverterScannerTests
   {
      /// <summary>
      /// The subject under test.
      /// </summary>
      private ConverterScanner _sut;

      /// <summary>
      /// Initializes a new instance of the <see cref="EasybankTests" /> class.
      /// </summary>
      public ConverterScannerTests()
      { 
      }

      /// <summary>
      /// Tests the setup.
      /// </summary>
      [TestFixtureSetUp]
      public void TestSetup()
      {
         _sut = new ConverterScanner();
      }

      /// <summary>
      /// Every converter shall have a ConverterNameAttribute to set the name.
      /// The name is used on the UI, so a user can identify the specific converter.
      /// </summary>
      [Test]
      public void ConverterScanner_EveryConverterItemShallContainName()
      {
         // Given
         var converters = _sut.ScanAssemblyForConverters();

         // Then
         foreach (var converterItem in converters)
         {
            Assert.That(converterItem.Name, Is.Not.Null, "Converter Name is null of " + converterItem.Converter.GetType());
            Assert.That(converterItem.Name, Is.Not.Empty, "Converter Name is empty of " + converterItem.Converter.GetType());
         }
      }

      /// <summary>
      /// Test if a Betterment record is converted to a ynab record.
      /// </summary>
      [Test]
      public void ConverterScanner_ShallReturnConverterItemForEasybank()
      {
         // Given

         // When
         var converters = _sut.ScanAssemblyForConverters();

         // Then

         var item = converters.Find(x => x.Name == "Easybank");

         Assert.That(item, Is.Not.Null);
         Assert.That(item.Converter, Is.TypeOf(typeof(EasybankConverter)));
      }

      /// <summary>
      /// A converter can have an author attribute like the "Betterment" converter.
      /// The converterItem shall contain that information.
      /// </summary>
      [Test]
      public void ConverterScanner_BettermentConverterItemShallContainAuthor()
      {
         // Given
         var converters = _sut.ScanAssemblyForConverters();

         // Then
         var item = converters.Find(x => x.Name == "Betterment");

         Assert.That(item.Author, Is.Not.Null, "Converter Author is null of " + item.Converter.GetType());
         Assert.That(item.Author, Is.Not.Empty, "Converter Author is empty of " + item.Converter.GetType());
         Assert.That(item.Author, Is.EqualTo("by friedrickjoel"));
      }
   }
}
